//权限弹窗
(function(){
    $('.addroom').click(function(){
        console.log($.cookie('usertoken'));
        console.log($.cookie('usertoken') != null);
        if($.cookie('usertoken') == 'null'){
            //true
            $('.warning.no_login').show();
            $('.warning.addroom').hide();
        }else{
            $('.warning.addroom').show();
            $('.warning.no_login').hide();
        }
        // $('.warning.addroom').show();
    });
    $('.warning').click(function(){
        $('.warning').hide();
    });
    $('.warningbox').click(function () {
        event.stopPropagation?event.stopPropagation():window.event.cancelBubble = true;
    });
    $('.zf_box').click(function () {
        event.stopPropagation?event.stopPropagation():window.event.cancelBubble = true;
    });
    $('.newroombox').click(function () {
        event.stopPropagation?event.stopPropagation():window.event.cancelBubble = true;
    });
    $(document).on('click','.qiandaobox',function () {
        event.stopPropagation?event.stopPropagation():window.event.cancelBubble = true;
    });
    $('.qiandaobox a.qiandao-close').click(function () {
        $('.warning.qiandao').hide();
    });
    $('.success .qiandao').click(function () {
        $('.warning.qiandao').show();
    })
})();
//聊天室侧边伸缩窗口
function fixedshow(name){
    $('.room-fixed-box').animate({right:'0'},300);
    $('.'+name).show();
};
$('.room-fixed-box a.close').click(function(){
    $('.room-fixed-box').animate({right:'-340px'},300,function(){
        $('.fixed-box').hide();
    });
});
//表情包开关
(function(){
   var emoji = true;
   $('.emoji').click(function(){
       console.log($('.room-video').is(':hidden'));
       if( $('.room-video').is(':hidden')){
           if(emoji){
               $('.messagebox').animate({height:'510px'},300);
               $(this).children('img').attr('src','images/icon_bqb_selected.png');
               $('.room-chating .message-bottom').animate({bottom:'190px'},300);
               $('.room-chating .message-bottom').scrollTop($(document).height()-190);
           }else{
               $('.messagebox').animate({height:'730px'},300);
               $(this).children('img').attr('src','images/icon_bqb.png');
               $('.room-chating .message-bottom').animate({'bottom':'-10px'},300)
           }
           emoji=!emoji;
       }else{
           if(emoji){
               $('.messagebox').animate({height:'207px'},300);
               $(this).children('img').attr('src','images/icon_bqb_selected.png');
               $('.room-chating .message-bottom').animate({bottom:'190px'},300);
               $('.room-chating .message-bottom').scrollTop($(document).height()-190);
           }else{
               $('.messagebox').animate({height:'373px'},300);
               $(this).children('img').attr('src','images/icon_bqb.png');
               $('.room-chating .message-bottom').animate({'bottom':'-10px'},300)
           }
           emoji=!emoji;
       }

   });
   $('.text-input').focus(function(){
       if($('.room-video').is(':hidden')){
           $('.messagebox').animate({height:'730px'},300);
           $('.chat-list').css({height:'730px'});
           $('.room-chating .message-bottom').animate({'bottom':'-10px'},300);
           $('a.emoji img').attr('src','images/icon_bqb.png');
           emoji= true;
       }else{
           $('.messagebox').animate({height:'373px'},300);
           $('.room-chating .message-bottom').animate({'bottom':'-10px'},300);
           $('a.emoji img').attr('src','images/icon_bqb.png');
           emoji= true;
       }
   });
})();
//login
$('.reg-from-box.loginbox .reg-btn').click(function(){
    denglu();
});
$(document).keyup(function(eventTag){
    var event=eventTag||windows.event;
    var e=event.srcElement||event.target;
    if(event.keyCode ==13){
        denglu();
    }
});
function denglu(){
    var zh = $('.zhanghao').val();
    var mm = $('.password').val();
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(zh)) {
        $('.login-input .zhanghao').addClass('redall');
        return false;
    } else if(mm==""){
        $('.login .password').addClass('redall');
        return false;
    }else{
        $('.login-input .zhanghao').removeClass('redall');
        $('.login-input .password').removeClass('redall');
        $.ajax({
            type:'POST',
            url:'http://shishi2017.com/api/index/login',
            data:{mobile:zh,password:mm},
            datatype:'json',
            success:(function(data){
                var succ = JSON.parse(data);
                console.log(succ);
                if(succ.ret==200){
                    $.cookie("user_token",succ.data.token,{path:'/'});
                    $.cookie("user_head",succ.data.head,{path:'/'});
                    $.cookie("user_id",succ.data.id,{path:'/'});
                    $.cookie("user_nickname",succ.data.nickname,{path:'/'});
                    $.cookie("other_login",succ.ret,{path:'/'});
                    $.cookie("user_r_token",succ.data.r_token,{path:'/'});
                    $.cookie("user_sign",succ.data.user_sig,{path:'/'});
                    $.ajax({
                        type: 'POST',
                        url: 'http://shishi2017.com/api/home/get_qiniu_token',
                        data: {uid: $.cookie('user_id'), token: $.cookie('user_token')},
                        datatype: 'json',
                        success: (function (ed) {
                            var ed = JSON.parse(ed);
                            $.cookie('qtoken',ed.data.token,{path:'/'});
                            $.cookie('qdm',ed.data.domain,{path:'/'});
                            succ.ret==200?$(window).attr('location','../index.html'):alert(succ.msg);
                        })
                    });



                }else{
                    alert('用户名或者密码错误!请确认后重试');
                }
            }),
            error:(function(eee){
                console.log(eee);
            })
        });
    }
}
$(window).ready(function(){
    if($.cookie('user_token')!=null&&$.cookie('user_token')!='null'){
        $('.login').hide();
        $('.success').show();
    }else{}
    //退出登陆
    $('.loginout').click(function(){
        $.cookie("user_token",null,{path:'/'});
        $.cookie("user_head",null,{path:'/'});
        $.cookie("user_id",null,{path:'/'});
        $.cookie("user_nickname",null,{path:'/'});
        $.cookie("user_r_token",null,{path:'/'});
        $.cookie("user_sign",null,{path:'/'});
        $.cookie("old_room_id",null,{path:'/'});
        $.cookie("qdm",null,{path:'/'});
        $.cookie("qtoken",null,{path:'/'});
        $.cookie("room_id",null,{path:'/'});
        $.cookie("user_sign",null,{path:'/'});
        $.cookie("room_keyword",null,{path:'/'});
        $('.login.success').hide();
        window.location.reload();
    });
});


//获取验证码通用
(function(){
    var off = true;
    $('.hqyzm').click(function(){
        var reg_phone_test=/^[1][3,4,5,7,8][0-9]{9}$/;
        var t = 60;
        if(off && reg_phone_test.test( $(this).siblings('input').val())){
            off=false;
            $(this).html(t+"s重试");
            $('.hqyzm').css({background:'#ccc',cursor:'default'});
            clearInterval(time);
            var time = setInterval(function(){
                if (t!==0){
                    t--;
                    $('.hqyzm').html(t+"s重试");
                    $('.hqyzm').css({background:'#ccc',cursor:'not-allowed'});
                } else{
                    $('.hqyzm').html("获取验证码");
                    clearInterval(time);
                    $('.hqyzm').css({background:'#1a9df7',cursor:'pointer'});
                    off=true;
                };
            },1000)
        }else{
            return false;
        }
    });
})();

(function(){
    var yzm='';
    var reg_phone_test=/^[1][3,4,5,7,8][0-9]{9}$/;
    var reg_phone=$('.reg_phone').val();
    var reg_yz=$('.reg_yz').val();
    var set_pwd=$('.set_pwd').val();
    var repeat_pwd = $('.repeat_pwd').val();
    var shenfen = $("input:radio:checked").val();
    var yz;
    //获取验证码
    $('.hqyzm').click(function(){
        var reg_phone=$('.reg_phone').val()
        if (!reg_phone_test.test(reg_phone)){
            $('.reg-yz-phone').addClass('redbottom');
        }else{
            $('.reg-yz-phone').removeClass('redbottom');
            $.ajax({
                url:'http://shishi2017.com/api/index/send_message',
                type:'post',
                datatype:'json',
                data:{mobile:reg_phone},
                success:(function (ress) {
                    if(JSON.parse(ress).ret!=200){
                        alert(JSON.parse(ress).msg)
                    }
                }),
                error:(function(ede){
                        alert(ede.msg);
                    })
            });
        }

    });
//注册
    $('.btn-zc').click(function(){
        var reg_phone=$('.reg_phone').val();
        var newreg = /^[\u4e00-\u9fa5_a-zA-Z0-9]+$/;
        if(!reg_phone_test.test(reg_phone)){    //手机号判断
            $('.reg-yz-phone').addClass('redbottom');
            return false
        }else if($('.reg_nickname').val()==''||$('.reg_nickname').val().length>10||!newreg.test($('.reg_nickname').val())){
            $('.reg-yz-nickname').addClass('redbottom');
            $('.reg-yz-nickname').append('<span>10个字符以内，且不包括特殊符号</span>')
            return false
        }else if($('.reg_yz').val()==''){ //验证码
            $('.reg-yz-yzm').addClass('redbottom');
            return false
        }else if($('.set_pwd').val()==''|| $('.set_pwd').val().length<8||$('.set_pwd').val().length>16){
            $('.reg-yz-pwd').addClass('redbottom');
            return false
        }else if($('.repeat_pwd').val()!=$('.set_pwd').val()){
            $('.reg-yz-pwd-repeat').addClass('redbottom');
            return false
        }else{
            $.ajax({
                url:'http://shishi2017.com/api/index/register',
                type:'POST',
                datatype:'json',
                data:{nickname:$('.reg_nickname').val(),mobile:$('.reg_phone').val(),code:$('.reg_yz').val(),password:$('.set_pwd').val()},
                success:(function(s){
                    var ss = JSON.parse(s);
                    if (ss.ret==200){
                        alert(ss.msg);
                        $(window).attr('location','login.html');
                    }else{
                        alert(ss.msg);
                    }
                })
            })
        }
    });
//    找回密码
    $('.lost-pwd').click(function(){
        var lphone = $('.lost-phone').val(),
            lyzm = $('.lost-yzm').val(),
            lmm = $('.lost-mm').val();
        if(lphone==''){
            $('.reg-yz-phone').addClass('redbottom');
            return false
        }else if(!reg_phone_test.test(lphone)){
            $('.reg-yz-phone').addClass('redbottom');
            return false
        }else if(lyzm==''){
            $('.reg-yz-yzm').addClass('redbottom');
            return false
        }else if(lmm==''|| lmm.length<8 || lmm.length>16 ){
            $('.reg-yz-pwd').addClass('redbottom');
            return false
        }else{
            $('.reg-yz-phone').removeClass('redbottom');
            $('.reg-yz-yzm').removeClass('redbottom');
            $('.reg-yz-pwd').removeClass('redbottom');
            $.ajax({
                url:'http://shishi2017.com/api/index/forget_password',
                type:'POST',
                datatype:'json',
                data:{
                    mobile:lphone,
                    password:lmm,
                    code:lyzm
                },success:(function(data){
                    var data = JSON.parse(data);
                    if(data.ret==200){
                        alert('密码修改成功，请重新登陆!');
                        $(location).attr('href', 'login.html');
                    }else{
                        alert(data.msg);
                    }
                }),error:(function(data){
                    alert(data);
                })
            });
            console.log(lphone, lyzm, lmm);
        }
    });
    // 房间搜索
    $('input.search').keyup(function (){
        var sea = $('input.search').val();
        if(sea!=''){
            setTimeout(function(){
                $.ajax({
                    url:'http://shishi2017.com/api/home/get_chatroom_info',
                    type:'POST',
                    datatype:'json',
                    data:{
                        uid:$.cookie('user_id'),
                        token:$.cookie('user_token'),
                        key_words:sea,
                    },
                    success:(function(msg){
                        var msg = JSON.parse(msg);
                        console.log(msg.data.back_data);
                        if (msg.ret==200){
                            $('.result_box').show();
                            var str='';
                            for(num in msg.data.back_data){
                                    str +=
                                    '<a class="result-btn" href="javascript:;" data-roomid="'+msg.data.back_data[num].uid+'">'+
                                    '<div class="result-img fl">\n' +'<img src="'+msg.data.back_data[num].back_url+'" alt="">\n' +
                                    '                        </div>\n' +
                                    '                        <div class="result-info fl">\n' +
                                    '                            <p>房间号：'+msg.data.back_data[num].room_number+'</p>\n' +
                                    '                            <p>关键词：'+msg.data.back_data[num].key_words+'</p>\n' +
                                    '                        </div>\n' +
                                    '                        <div class="clearfix"></div>\n'+
                                    '</a>';
                            }
                            $('.result_box').html(str);
                        } else{
                            $('.result_box').html('<p style="text-align: center">暂无搜索结果</p>');
                        }

                    })
                })
            },200)
        }else{
            $('.result_box').hide();
            $('.result_box').html('');
        }
    });
    $('input.search').blur(function (){
        $('.result_box').hide();
    });
})();



